package Avocado.form {

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class FormLabel extends MovieClip {
		
		public static const INVERT_LABEL_SIDE:String = new String('invertLabelSide');
		public static const LABEL_IN_CENTER:String = new String('labelInCenter');
		
		private var textLabel:String;
		private var labelSide:String;
		
		private var format:TextFormat;
		public var field:TextField = new TextField;
		
		public var visualMask:Sprite;
		public var visualBackground:Sprite;

		public function FormLabel(_format:TextFormat, _textLabel:String='', _labelSide:String='') {
			super();
			this.format = _format;
			this.tabEnabled = false;
			this.textLabel = _textLabel;
			this.labelSide = _labelSide;
			this.addEventListener(Event.ADDED_TO_STAGE, initThis);
		}
		
		private function initThis(e:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, initThis);
			setupVisualBackground();
			setupField();
			setupVisualMask();
		}

		
		/**
		 * setup textfield value and auto validate the text
		 * @_value : String 
		 * ***/
		public function set textValue(_value:String):void{
			field.text = _value;
			updateVisualMask();
		}
		public function get textValue():String{
			return field.text.toString();
		}

		private function setupVisualBackground():void{
			visualBackground = new Sprite;
			visualBackground.alpha = 0;
			addChild(visualBackground);
		}
			private function updateVisualBackground():void{
				visualBackground.x = field.x;
				visualBackground.y = field.y;
				visualBackground.graphics.clear();
				visualBackground.graphics.beginFill(0xffffff);
				visualBackground.graphics.drawRect(getRect(field).x, getRect(field).y, getRect(field).width, getRect(field).height);
				visualBackground.graphics.endFill();
			}
		
		
		private function setupField():void{
			field.text = textLabel;
			field.embedFonts = true;
			field.autoSize = TextFieldAutoSize.LEFT;
			field.defaultTextFormat = format;
			field.setTextFormat(format);
			field.selectable = false;
			
			updateFieldPosition();
			
			addChild(field);
			
			updateVisualBackground();
		}
			private function updateFieldPosition():void{
				switch (labelSide) {
					case INVERT_LABEL_SIDE:
						field.x = 13;
						break;
					case LABEL_IN_CENTER:
						field.autoSize = TextFieldAutoSize.CENTER;
						field.x = -(field.textWidth/2);
						break;
					default:
						field.x = -(field.textWidth) - 13;
						break;
				}
				field.y = 3;
			}
		
		
		private function setupVisualMask():void{
			visualMask = new Sprite;
			visualMask.graphics.beginFill(0xffffff);
			visualMask.graphics.drawRect(getRect(field).x, getRect(field).y, getRect(field).width, getRect(field).height);
			visualMask.graphics.endFill();
			visualMask.x = field.x;
			visualMask.y = field.y;
			visualMask.alpha = 0;
			addChild(visualMask);
		}
			private function updateVisualMask():void{
				visualMask.graphics.clear();
				visualMask.graphics.beginFill(0xffffff);
				visualMask.graphics.drawRect(getRect(field).x, getRect(field).y, getRect(field).width, getRect(field).height);
				visualMask.graphics.endFill();
			}
		
	}
}